//
//  HomeView.m
//  Custom_Weibo
//
//  Created by LEO on 12/11/15.
//  Copyright © 2015 Leo_Lei. All rights reserved.
//

#import "HomeView.h"

@implementation HomeView

#pragma mark - INITIALIZATION_METHODS
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self buildSubView];
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self buildSubView];
    }
    return self;
}

#pragma mark SET_MODEL
- (void)setLayoutFrame:(WeiboViewLayoutFrame *)layoutFrame {
    if (_layoutFrame != layoutFrame) {
        _layoutFrame = layoutFrame;
        [self setNeedsLayout];
    }
}

#pragma mark REARRANGE_VIEWS
- (void)layoutSubviews {
    [super layoutSubviews];
    WeiboModel *_weiboModel = _layoutFrame.weiboModel;
    
    //  1)Whole weibo view rearrange.
//    self.frame = _layoutFrame.mainFrame;

    //  2)Original weibo text.
    _textLabel.frame = _layoutFrame.textFrame;
    _textLabel.text = _weiboModel.text;
    
    /*  3)Make sure if there is any repost weibo.If so, show the repost view, or
    make it invisible.
     */
    if (_weiboModel.repostWeiboModel == nil) {
        _repostTextLabel.hidden = YES;
        _bgImageView.hidden = YES;
        
        /*  Judged whether the original weibo has pictures.If so, show the image,
         or make it invisible.
         */
        if (_weiboModel.thumbnailImage != nil) {
            _imageView.hidden = NO;
            _imageView.frame = _layoutFrame.imageFrame;
            
            //  Set the thumbImage and its original image.
            NSString *thumbImageString = _weiboModel.thumbnailImage;
            [_imageView sd_setImageWithURL:[NSURL URLWithString:thumbImageString]];
            
            /*  There should be set original image url when you
             tap for detail image.
             */
            _imageView.originImageUrl = _weiboModel.originalImage;
        }else {
            _imageView.hidden = YES;
        }
    }else {
        //  1)Background image
        _bgImageView.hidden = NO;
        _bgImageView.frame = _layoutFrame.backgroundImageFrame;
        
        //  2)Repost text
        _repostTextLabel.hidden = NO;
        _repostTextLabel.frame = _layoutFrame.repostTextFrame;
        _repostTextLabel.text = _weiboModel.repostWeiboModel.text;
        
        //  3)Repost image
        /*  Judged whether the repost weibo has pictures.If so, show the image,
         or make it invisible.
         */
        if (_weiboModel.repostWeiboModel.thumbnailImage != nil) {
            _imageView.hidden = NO;
            _imageView.frame = _layoutFrame.imageFrame;
            
            //  Set the thumbImage and its original image.
            NSString *repostThumbImageString = _weiboModel.repostWeiboModel.thumbnailImage;
            [_imageView sd_setImageWithURL:[NSURL URLWithString:repostThumbImageString]];
            
            /*  There should be set original image url when you
             tap for detail image.
             */
            _imageView.originImageUrl = _weiboModel.repostWeiboModel.originalImage;
        }else {
            _imageView.hidden = YES;
        }
    }
    
    if (_imageView.hidden == NO) {
        NSString *extension;
        _imageView.gifIconView.frame = CGRectMake(_imageView.width - 24, _imageView.height - 14, 24, 14);
        
        if (_weiboModel.repostWeiboModel == nil) {
            extension = [_weiboModel.thumbnailImage pathExtension];
        }else {
            extension = [_weiboModel.repostWeiboModel.thumbnailImage pathExtension];
        }
        
        if ([extension isEqualToString:@"gif"]) {
            _imageView.gifIconView.hidden = NO;
        }else {
            _imageView.gifIconView.hidden = YES;
        }
    }
}

#pragma mark - DESIGN_SUBVIEW
- (void)buildSubView {
    _textLabel = [[WXLabel alloc] initWithFrame:CGRectZero];
    _textLabel.font = [UIFont systemFontOfSize:15];
    _textLabel.linespace = 5.0f;
    _textLabel.wxLabelDelegate = self;
    
    _repostTextLabel = [[WXLabel alloc] initWithFrame:CGRectZero];
    _repostTextLabel.font = [UIFont systemFontOfSize:14];
    _repostTextLabel.linespace = 5.0f;
    _repostTextLabel.wxLabelDelegate = self;
    
    _imageView = [[TapImageView alloc] initWithFrame:CGRectZero];
    
    _bgImageView = [[ThemeImageView alloc] initWithFrame:CGRectZero];
    _bgImageView.topCapHeight = 30;
    _bgImageView.leftCapWidth = 30;
    _bgImageView.imageName = @"timeline_rt_border_9.png";
    
    [self addSubview:_bgImageView];
    [self addSubview:_textLabel];
    [self addSubview:_repostTextLabel];
    [self addSubview:_imageView];
}

#pragma mark - WXLABEL_DELEGATE
//  Set different color for special text.
- (NSString *)contentsOfRegexStringWithWXLabel:(WXLabel *)wxLabel {
    NSString *regexUser = @"@\\w+";
    NSString *regexLink = @"http(s)?://([a-zA-Z0-9._-]+(/)?)*";
    NSString *regexTopic = @"#\\w+#";
    
    NSString *regex = [NSString stringWithFormat:@"%@|%@|%@", regexUser, regexLink, regexTopic];
    return regex;
}

//  Choose the color for special text.
- (UIColor *)linkColorWithWXLabel:(WXLabel *)wxLabel {
    return kRGB(243, 52, 23, 0.6);
}

//  Choose the color for special text when the text is being pressed.
- (UIColor *)passColorWithWXLabel:(WXLabel *)wxLabel {
    return kRGB(0, 255, 50, 0.6);
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
